# gozerlib/plugs/admin.py
#
#

""" admin related commands. """

## gozerlib imports

from gozerlib.commands import cmnds
from gozerlib.examples import examples
from gozerlib.persist.persist import Persist
from gozerlib.aliases import aliasreverse
from gozerlib.admin import savecmndtable, savepluginlist, plugin_packages
from gozerlib.config import cfg
from gozerlib.plugins import plugs

## simplejson imports

from waveapi.simplejson import dump

def handle_init(bot, ievent):
    plugs.loadall(force=True)
    savecmndtable()
    savepluginlist()
    cfg['ondemand'] = True
    ievent.done()

cmnds.add('admin-init', handle_init, 'OPER')
examples.add('admin-init', 'initialize the bot .. cmndtable and pluginlist', 'admin-init')

def handle_loadall(bot, ievent):
    plugs.loadall(force=True)
    ievent.done()

cmnds.add('admin-loadall', handle_loadall, 'OPER')
examples.add('admin-loadall', 'load all plugins', 'admin-loadall')

def handle_flushcache(bot, ievent):

    """ flush the cache .. flush all with no arguments otherwise deleted the item. """
   
    from google.appengine.api.memcache import flush_all, delete

    if not ievent.rest:
        flush_all()
    else:
        delete(ievent.rest)

    ievent.done()

cmnds.add('admin-flushcache', handle_flushcache, 'OPER')
examples.add('admin-flushcache', 'flush the bots cache', 'admin-flushcache')

def handle_adminstats(bot, ievent):

    """ show bot stats. """
   
    from google.appengine.api.memcache import get_stats

    ievent.reply("cache: %s" % str(get_stats()))

cmnds.add('admin-stats', handle_adminstats, 'OPER')
examples.add('admin-stats', 'show bots stats', 'admin-stats')
